<?php
class Service_Amail {
	var $email_to;
	var $email_subject;
	var $headers;
	var $mime_boundary;
	var $email_message;
	
	//sets up variables and mail email
	function Service_Amail($email_to, $email_subject, $email_message, $headers) {
		$this->email_to = $email_to;
		$this->email_subject = $email_subject;
		$this->headers = $headers;
		$semi_rand = md5(time());
		$this->mime_boundary = "==Multipart_Boundary_x" . $semi_rand . "x";
		$this->headers .= "MIME-Version: 1.0\n" . 
		"Content-Type: multipart/mixed;\n" . 
		" boundary=\"" . $this->mime_boundary . "\"";
		$this->email_message .= "This is a multi-part message in MIME format.\n\n" . 
			"--{$this->mime_boundary}\n" . 
			"Content-Type:text/html; charset=\"utf-8\"\n" . 
			"Content-Transfer-Encoding: base64\n" . 
			base64_encode($email_message) . "\n";
	}
	
	//adds attachment
	function attach($fileatt_type, $fileatt_name, $fileatt_content) {
		$data = chunk_split(base64_encode($fileatt_content));
		$this->email_message .= "--" . $this->mime_boundary . "\n" . 
			"Content-Type: " . $fileatt_type. ";\n" . 
			" name=\"" . $fileatt_name . "\"\n" . 
			"Content-ID: <" . $fileatt_name . ">\n" . 
			"Content-Transfer-Encoding: base64\n" . 
			$data . "\n"; 
			//"--{$this->mime_boundary}\n";
		unset($data);
		unset($fileatt_type);
		unset($fileatt_name);
	}
	
	//send email
	function send() {
		Service_Debug::setDebug();
	    // Сохраняем сообщение на всякий случай
      $strToFile = "<html>";
      $strToFile .= '<meta charset="utf-8" />';
      $strToFile .= "<h2>ЗАГОЛОВКИ:</h2>";
      $strToFile .= $this->headers;
      $strToFile .= "<br />=============";
      $strToFile .= "<h2>КОМУ:</h2>";
      $strToFile .= $this->email_to;
      $strToFile .= "<br />=============";
      $strToFile .= "<h2>ТЕМА:</h2>";
      $strToFile .= $this->email_subject;
      $strToFile .= "<br />=============";
      $strToFile .= "<h2>СООБЩЕНИЕ:</h2>";
      $strToFile .= $this->email_message;
      $strToFile .= "<br />=============<br /></html>";
      $f = fopen(ABSPATH . 'content/mail/' . date("Y_m_d__H_i_s") . '.htm', 'w');
      fwrite($f, $strToFile);
      fclose($f);    
    
    //Отправляем сообщение
    // Если отправка идет на почту
    if ( ! defined( 'MAIL_CLASS_DEFINED' ) ) {
	    return mail($this->email_to, $this->email_subject, $this->email_message, $this->headers);
    }		
	}

	//send email with imap
	function imap_send() {
		return imap_mail($this->email_to, $this->email_subject, $this->email_message, $this->headers);
	}
	
	//read file and add as attachment
	function file($file) {
		$o = fopen($file, "rb");
		$content = fread($o, filesize($file));
		fclose($o);
		$name = basename($file);
		$type = "application/octet-stream";
		$this->attach($type, $name, $content);
	}
	
	//read directory and add files as attachments
	function dir($dir) {
		$o = opendir($dir);
		while ( ($file = readdir($o)) !== false ) {
			if ($file != "." && $file != "..") {
				if (is_dir($dir . "/" . $file)) {
					$this->dir($dir . "/" . $file);
				}
				else {
					$this->file($dir . "/" . $file);
				}
			}
		}
	}
	
	/**
	 * Возвращает электронный адрес пользователя вида Иванов Иван <aa@bb.ru>
	 *  
	 * @param array $arrUser - инфа о пользователе
	 */
	public static function getUserEMail($arrUser) {
		Service_Debug::setDebug();
		if (empty($arrUser['e_mail'])) return false;
		if (empty($arrUser['last_name']) || empty($arrUser['first_name'])) return $arrUser['e_mail'];
		else return $arrUser['last_name'] . ' ' . $arrUser['first_name'] . ' <' . $arrUser['e_mail'] . '>';
	}

}
?> 